﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmMenu : MeForm
    {
        TreeNode selectNode;
        MessageNode nodes;
        bool isAccpet = false;
        bool isSerializer = false;
        bool isSave = true;
        internal MessageNode Nodes { get { return nodes; } }
        private frmMenu()
        {
            InitializeComponent();
            this.Text = "菜单导航管理";
            //DataFactory.GetInitialization();
            this.treeView.HideSelection = false;
            this.kryptonGroupBox1.Text = "菜单导航";
            //NodeFactory.SetAllDataToTreeView(this.treeViewNode);
            treeView.AllowDrop = true;
            this.Load += new EventHandler(frmMessage_Load);
            this.FormClosing += new FormClosingEventHandler(frmMessage_FormClosing);
            this.treeView.ItemDrag += new ItemDragEventHandler(treeView_ItemDrag);
            this.treeView.DragDrop += new DragEventHandler(treeView_DragDrop);
            this.treeView.DragEnter += new DragEventHandler(treeView_DragEnter);
            this.treeView.QueryContinueDrag += new QueryContinueDragEventHandler(treeView_QueryContinueDrag);
            this.treeView.DragOver += new DragEventHandler(treeView_DragOver);
            this.treeView.GiveFeedback += new GiveFeedbackEventHandler(treeView_GiveFeedback);
            this.treeView.DragLeave += new EventHandler(treeView_DragLeave);
        }

        public frmMenu(MessageNode node)
            : this()
        {
            InitMenu(node);
        }

        void InitMenu(MessageNode node)
        {
            if (node == null)
            {
                this.nodes = new MessageNode();
            }
            else
            {
                this.nodes = node;
                NodeFactory.ToTreeView(node.Nodes, treeView);
                txtMenuTitle.Text = nodes.Title;
            }
        }

        public frmMenu(MessageNode nodes, bool isSave)
            : this(nodes)
        {
            this.isSave = isSave;   
        }
         
        #region TreeView 事件

        TreeNode dragDropTreeNode;
        DateTime startTime;

        void treeView_DragLeave(object sender, EventArgs e)
        {
            if (this.dragDropTreeNode != null) //在按下{ESC}，取消被放置的节点高亮显示
            {
                this.dragDropTreeNode.BackColor = SystemColors.Window;
                this.dragDropTreeNode.ForeColor = SystemColors.WindowText;
                this.dragDropTreeNode = null;
            }
        }

        //随着鼠标的移动显示不同的光标效果
        void treeView_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            //e.UseDefaultCursors = true;
            // Use custom cursors if the check box is checked.
            //if (UseCustomCursorsCheck.Checked)
            //{
            //int i = 0;
            // Sets the custom cursor based upon the effect.
            //e.UseDefaultCursors = false;
            if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
            {
                //Cursor d = new Cursor("");
                //Cursor.Current = MyNormalCursor;
            }
            else
            {
                //Cursor.Current = MyNoDropCursor;
            }
            //}
        }

        void treeView_DragOver(object sender, DragEventArgs e)
        {
            //当光标悬停在 TreeView 控件上时，展开该控件中的 TreeNode
            Point p = this.treeView.PointToClient(Control.MousePosition);
            TreeNode tn = this.treeView.GetNodeAt(p);
            if (tn != null)
            {
                if (this.dragDropTreeNode != tn) //移动到新的节点
                {
                    if (tn.Nodes.Count > 0 && tn.IsExpanded == false)
                    {
                        this.startTime = DateTime.Now;//设置新的起始时间
                    }
                }
                else
                {
                    if (tn.Nodes.Count > 0 && tn.IsExpanded == false && this.startTime != DateTime.MinValue)
                    {
                        TimeSpan ts = DateTime.Now - this.startTime;
                        if (ts.TotalMilliseconds >= 1000) //一秒
                        {
                            tn.Expand();
                            this.startTime = DateTime.MinValue;
                        }
                    }
                }
            }

            //设置拖放标签Effect状态
            if (tn != null)//&& (tn != this.treeView.SelectedNode)) //当控件移动到空白处时，设置不可用。
            {
                if ((e.AllowedEffect & DragDropEffects.Move) != 0)
                {
                    e.Effect = DragDropEffects.Move;
                }
                if (((e.AllowedEffect & DragDropEffects.Copy) != 0) && ((e.KeyState & 0x08) != 0))//Ctrl key
                {
                    e.Effect = DragDropEffects.Copy;
                }
                if (((e.AllowedEffect & DragDropEffects.Link) != 0) && ((e.KeyState & 0x08) != 0) && ((e.KeyState & 0x04) != 0))//Ctrl key + Shift key
                {
                    e.Effect = DragDropEffects.Link;
                }
                if (e.Data.GetDataPresent(typeof(TreeNode)))//拖动的是TreeNode
                {
                    TreeNode parND = tn;//判断是否拖到了子项
                    bool isChildNode = false;
                    while (parND.Parent != null)
                    {
                        parND = parND.Parent;
                        if (parND == this.treeView.SelectedNode)
                        {
                            isChildNode = true;
                            break;
                        }
                    }
                    if (isChildNode)
                    {
                        e.Effect = DragDropEffects.None;
                    }
                }
                else if (e.Data.GetDataPresent(typeof(ListViewItem)))//拖动的是ListViewItem
                {
                    if (tn.Parent == null)
                    {
                        e.Effect = DragDropEffects.None;
                    }
                }
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

            //设置拖放目标TreeNode的背景色
            if (e.Effect == DragDropEffects.None)
            {
                if (this.dragDropTreeNode != null) //取消被放置的节点高亮显示
                {
                    this.dragDropTreeNode.BackColor = SystemColors.Window;
                    this.dragDropTreeNode.ForeColor = SystemColors.WindowText;
                    this.dragDropTreeNode = null;
                }
            }
            else
            {
                if (tn != null)
                {
                    if (this.dragDropTreeNode != null)
                    {
                        if (this.dragDropTreeNode != tn)
                        {
                            this.dragDropTreeNode.BackColor = SystemColors.Window;//取消上一个被放置的节点高亮显示
                            this.dragDropTreeNode.ForeColor = SystemColors.WindowText;
                            this.dragDropTreeNode = tn;//设置为新的节点
                            this.dragDropTreeNode.BackColor = SystemColors.Highlight;
                            this.dragDropTreeNode.ForeColor = SystemColors.HighlightText;
                        }
                    }
                    else
                    {
                        this.dragDropTreeNode = tn;//设置为新的节点
                        this.dragDropTreeNode.BackColor = SystemColors.Highlight;
                        this.dragDropTreeNode.ForeColor = SystemColors.HighlightText;
                    }
                }
            }
        }

        void treeView_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            //例如，当光标悬停在 TreeView 控件上时，展开该控件中的 TreeNode
            //this.textBox1.Text = p.ToString();
        }

        void treeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            TreeNode tn = e.Item as TreeNode;
            if ((e.Button == MouseButtons.Left) && (tn != null) && (tn.Parent != null)) //根节点不允许拖放操作。
            //if ((e.Button == MouseButtons.Left) && (tn != null))//存在无法操作
            {
                this.treeView.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
            }
        }

        void treeView_DragEnter(object sender, DragEventArgs e)
        {
            //e.Effect = e.AllowedEffect;
        }
        
        void treeView_DragDrop(object sender, DragEventArgs e)
        {
            if (this.dragDropTreeNode != null)
            {
                if (e.Data.GetDataPresent(typeof(TreeNode)))
                {
                    TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode));
                    Node node = tn.Tag as Node;

                    if (tn.Parent != null)
                    {
                        if (tn.Parent.Tag as Node != null)//如果是子节点删除
                            (tn.Parent.Tag as Node).SubNode.Remove(node);
                    }
                    else
                        nodes.Nodes.Remove(node);//如果找不到上级节点，则用根节点删除
                    
                    tn.Remove();//从原父节点移除被拖得节点
                    this.dragDropTreeNode.Nodes.Add(tn);//添加被拖得节点到新节点下面
                    Node dragDropNode = this.dragDropTreeNode.Tag as Node;
                    if (dragDropNode != null)
                        dragDropNode.SubNode.Add(node);
                    if (this.dragDropTreeNode.Parent == null)
                    {
                    }
                    else
                    {
                    }
                    if (this.dragDropTreeNode.IsExpanded == false)
                    {
                        this.dragDropTreeNode.Expand();//展开节点
                    }
                }
                else if (e.Data.GetDataPresent(typeof(ListViewItem)))
                {
                    if (this.dragDropTreeNode.Parent != null)
                    {
                    }
                }

                //取消被放置的节点高亮显示
                this.dragDropTreeNode.BackColor = SystemColors.Window;
                this.dragDropTreeNode.ForeColor = SystemColors.WindowText;
                this.dragDropTreeNode = null;
            }
        }

        private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null)
            {
                EditMessage(e.Node);
            }
        }
        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            selectNode = e.Node;
            if (selectNode != null && selectNode.Nodes != null && selectNode.Nodes.Count <= 0)
            {
                
                //btnEdit.Enabled = true;
                //btnAddMessage.Enabled = true;
            }
            else
            {
                //btnAddMessage.Enabled = false;
               
                //btnEdit.Enabled = false;
            }
        }
        #endregion

        void CloseForm()
        {
            nodes.Title = string.IsNullOrEmpty(txtMenuTitle.Text) ? "菜单列表" : txtMenuTitle.Text;
            //if (!isSerializer)
            //{
            //    if (isSave == true)
            //        SerializerFactory.SerializeXml(nodes);
            //    isSerializer = true;
            //}
        }

        void frmMessage_Load(object sender, EventArgs e)
        {
            
        }

        private void btnAddNodeRoot_Click(object sender, EventArgs e)
        {
            AddRootNode();
        }
        
        private void btnAddNode_Click(object sender, EventArgs e)
        {
            if (selectNode == null) {
                MeMessageBox.ShowInformation("请选择节点再添加");
                return;
            }
            AddNode(selectNode);
        }

        #region 树操作

        void SetFocus(TreeNode treeNode)
        {
            treeView.SelectedNode = treeNode;
            treeView.Focus();
        }

        void AddRootNode()
        {
            frmMenuEdit input = new frmMenuEdit();
            DialogResult result = input.ShowDialog();
            TreeNode node = null;
            if (result == DialogResult.Yes)
            {
                Node root = new Node();
                node = new TreeNode(input.MessageInfo);
                root.Text = input.MessageInfo;
                node.Tag = root;
                treeView.Nodes.Add(node);
                nodes.Nodes.Add(root);
            }
            if (node != null)
                SetFocus(node);
        }

        void AddNode(TreeNode treeNode)
        {
            frmMenuEdit input = new frmMenuEdit();
            DialogResult result = input.ShowDialog();
            if (result == DialogResult.Yes)
            {
                Node root = treeNode.Tag as Node;
                Node rootNode = treeNode.Tag as Node;
                TreeNode temp = new TreeNode(input.MessageInfo);
                Node node = new Node();
                node.Text = input.MessageInfo;
                temp.Tag = node;
                if (root != null)
                {
                    root.SubNode.Add(node);
                }
                else if (rootNode != null)
                {
                    //rootNode.Text = input.MessageInfo;
                    rootNode.SubNode.Add(node);
                }
                treeNode.Nodes.Add(temp);
                SetFocus(temp);
            }
            //treeView.SelectedNode = treeNode;
            
        }

        void AddMessage(TreeNode treeNode)
        {
            MeTalker.Aide.data.Node myNode = selectNode.Tag as MeTalker.Aide.data.Node;
            if (treeNode != null)
            {
                //string keyword = if(myNode!=null)?myNode.Keyword?(rootNode!=null?rootNode.Key)
                frmMenuEdit edit = new frmMenuEdit();
                DialogResult result = edit.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    if (!string.IsNullOrEmpty(edit.MessageInfo))
                    {
                        MeTalker.Aide.data.Node nodeTemp = new Node() { Key = edit.MessageInfo, Text = edit.MessageInfo };
                        TreeNode treeNode1 = new TreeNode();
                        treeNode1.Text = nodeTemp.Text;
                        treeNode1.Tag = nodeTemp;
                        
                            myNode.SubNode.Add(nodeTemp);
                      
                        treeNode.Nodes.Add(treeNode1);
                        SetFocus(treeNode1);
                    }
                }
            }
           //treeView.SelectedNode = treeNode;
            
        }

        void EditMessage(TreeNode treeNode)
        {
            MeTalker.Aide.data.Node myNode = treeNode.Tag as MeTalker.Aide.data.Node;
            if (treeNode.Nodes != null && treeNode.Nodes.Count > 0)
            {
                frmMenuEdit edit = new frmMenuEdit(selectNode.Text);
                DialogResult result = edit.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    if (!string.IsNullOrEmpty(edit.MessageInfo))
                    {
                        treeNode.Text = edit.MessageInfo;
                        myNode.Text = edit.MessageInfo;
                        myNode.Key = edit.MessageInfo;
                    }
                }
            }
            else
            {
                frmMenuEdit edit = new frmMenuEdit(treeNode.Text);
                DialogResult result = edit.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    if (!string.IsNullOrEmpty(edit.MessageInfo))
                    {
                        treeNode.Text = edit.MessageInfo;
                        myNode.Text = edit.MessageInfo;
                        myNode.Key = edit.MessageInfo;
                    }
                }

            }
           // treeView.SelectedNode = treeNode;
            SetFocus(treeNode);
        }

        void DeleteMessage(TreeNode treeNode)
        {
            if (selectNode != null)
            {
                TreeNode nodeParent = selectNode.Parent;
                Node root = selectNode.Tag as Node;
                Node rootNode = selectNode.Tag as Node;

                int index = -1;
                bool flag = false;
                if (nodeParent == null)
                {
                    index = treeView.Nodes.IndexOf(selectNode);
                    treeView.Nodes.Remove(selectNode);
                    nodes.Nodes.Remove(root);
                }
                else
                {
                    flag = true;
                    index = nodeParent.Nodes.IndexOf(selectNode);
                    Node temp = nodeParent.Tag as Node;
                    nodeParent.Nodes.Remove(selectNode);
                    if (temp != null)
                        temp.SubNode.Remove(rootNode);
                }
                if (!flag)
                {
                    SetFocus(treeView.Nodes[index - 1]);
                }
                else
                {
                    if (index > 0)
                    {
                        SetFocus(nodeParent.Nodes[index - 1]);
                    }
                    else
                    {
                        SetFocus(nodeParent);
                    }
                }
            }
        }

        void MoveUpTreeNode()
        {
            if (selectNode != null && selectNode.Parent != null)
            {
                TreeNode parentTreeNode = selectNode.Parent;
                Node node = selectNode.Tag as Node;
                Node parentNode = parentTreeNode.Tag as Node;
                int index = parentNode.SubNode.IndexOf(node);
                parentNode.SubNode.Remove(node);
                TreeNode cloneTree = selectNode.Clone() as TreeNode;
                int treeIndex = selectNode.Parent.Nodes.IndexOf(selectNode);
                selectNode.Remove();
                parentTreeNode.Nodes.Insert(treeIndex - 1, cloneTree);
                parentNode.SubNode.Insert(index - 1, node);

            }
            else if (selectNode != null && selectNode.Parent == null)
            {
                TreeNode cloneTree = selectNode.Clone() as TreeNode;
                int treeIndex = treeView.Nodes.IndexOf(selectNode);
                Node dataNode = selectNode.Tag as Node;
                selectNode.Remove();
                int index = nodes.Nodes.IndexOf(dataNode);
                treeView.Nodes.Insert(treeIndex - 1, cloneTree);
                nodes.Nodes.Remove(dataNode);
                nodes.Nodes.Insert(index - 1, dataNode);
            }
            //treeView.SelectedNode = selectNode;
            SetFocus(selectNode);
        }

        void MoveDownTreeNode()
        {
            if (selectNode != null && selectNode.Parent != null)
            {
                TreeNode parentTreeNode = selectNode.Parent;
                Node node = selectNode.Tag as Node;
                Node parentNode = parentTreeNode.Tag as Node;
                int index = parentNode.SubNode.IndexOf(node);
                parentNode.SubNode.Remove(node);
                TreeNode cloneTree = selectNode.Clone() as TreeNode;
                int treeIndex = selectNode.Parent.Nodes.IndexOf(selectNode);
                selectNode.Remove();
                parentTreeNode.Nodes.Insert(treeIndex + 1, cloneTree);
                parentNode.SubNode.Insert(index + 1, node);

            }
            else if (selectNode != null && selectNode.Parent == null)
            {
                TreeNode cloneTree = selectNode.Clone() as TreeNode;
                int treeIndex = treeView.Nodes.IndexOf(selectNode);
                Node dataNode = selectNode.Tag as Node;
                selectNode.Remove();
                int index = nodes.Nodes.IndexOf(dataNode);
                treeView.Nodes.Insert(treeIndex + 1, cloneTree);
                nodes.Nodes.Remove(dataNode);
                nodes.Nodes.Insert(index + 1, dataNode);
            }
           //treeView.SelectedNode = selectNode;
            SetFocus(selectNode);
        }
        #endregion

        private void btnAddMessage_Click(object sender, EventArgs e)
        {
            if (selectNode == null)
            {
                MeMessageBox.ShowInformation("请选择节点再添加");
                return;
            }
            AddMessage(selectNode);
            //MeTalker.Aide.data.Node myNode = selectNode.Tag as MeTalker.Aide.data.Node;
            //MeTalker.Aide.data.NodeRoot rootNode = selectNode.Tag as MeTalker.Aide.data.NodeRoot;
            //if (rootNode != null)
            //{
            //  //  AddMessage(selectNode);
            //    return;
            //}

            //if (myNode != null)
            //{
            //    //if (selectNode.Nodes != null && selectNode.Nodes.Count > 0)
            //    //{

            //    frmTypeEdit edit = new frmTypeEdit(selectNode.Text, myNode.Keyword);
            //    DialogResult result = edit.ShowDialog();
            //    if (result == DialogResult.Yes)
            //    {
            //        if (!string.IsNullOrEmpty(edit.MessageInfo))
            //        {
            //            selectNode.Text = edit.MessageInfo;
            //            if (myNode != null)
            //            {
            //                myNode.Text = edit.MessageInfo;
            //                myNode.Key = edit.MessageInfo;
            //            }
            //            //else if (rootNode != null) {
            //            //    rootNode.Nodes.Add(new Node() { Text = edit.MessageInfo, Key = edit.MessageInfo });
            //            //}
            //        }
            //    }
            //}
            //}
            //else
            //{
            //    frmTypeInput edit = new frmTypeInput("子分类短语类别");
            //    DialogResult result = edit.ShowDialog();
            //    if (result == DialogResult.Yes)
            //    {
            //        if (!string.IsNullOrEmpty(edit.MessageInfo))
            //        {
            //            selectNode.Text = edit.MessageInfo;
            //            myNode.Text = edit.MessageInfo;
            //            myNode.Key = edit.MessageInfo;
            //        }
            //    }
            //}
        }
        
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (isAccpet)
            {
                DeleteMessage(selectNode);
                return;
            }
            if (MeMessageBox.ShowQuestion("确定删除短语吗,下次将不再提示") == DialogResult.Yes)
            {
                DeleteMessage(selectNode);
                isAccpet = true;
            }
        }

        void frmMessage_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseForm();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (selectNode == null)
            {
                MeMessageBox.ShowInformation("请选择节点再添加");
                return;
            }
            EditMessage(selectNode);

            //MeTalker.Aide.data.Node myNode = selectNode.Tag as MeTalker.Aide.data.Node;
            //if (myNode != null)
            //{
            //    if (selectNode.Nodes != null && selectNode.Nodes.Count > 0)
            //    {

            //        frmTypeInput edit = new frmTypeInput("修改分类短语类别",selectNode.Text);
            //        DialogResult result = edit.ShowDialog();
            //        if (result == DialogResult.Yes)
            //        {
            //            if (!string.IsNullOrEmpty(edit.MessageInfo))
            //            {
            //                selectNode.Text = edit.MessageInfo;
            //                myNode.Text = edit.MessageInfo;
            //                myNode.Key = edit.MessageInfo;
            //            }
            //        }
            //    }
            //    else
            //    {
            //        frmTypeEdit edit = new frmTypeEdit(selectNode.Text, myNode.Keyword);
            //        DialogResult result = edit.ShowDialog();
            //        if (result == DialogResult.Yes)
            //        {
            //            if (!string.IsNullOrEmpty(edit.MessageInfo))
            //            {
            //                selectNode.Text = edit.MessageInfo;
            //                myNode.Text = edit.MessageInfo;
            //                myNode.Key = edit.MessageInfo;
            //            }
            //        }

            //    }
            //}
        }

        private void btnKey_Click(object sender, EventArgs e)
        {
            if (selectNode != null)
                new frmKeyword(selectNode.Tag as Node).ShowDialog();
        }

        private void btnUpMove_Click(object sender, EventArgs e)
        {
            MoveUpTreeNode();
        }
 
        private void btnDownMove_Click(object sender, EventArgs e)
        {
            MoveDownTreeNode();
        }

        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeView.SelectedNode = e.Node;
        }

      
        private TreeNode FindNode(TreeNode tnParent, string strValue)
        {
            if (tnParent == null) return null;

            //if (tnParent.Text == strValue) return tnParent;
            if (tnParent.Text.Contains(strValue)) return tnParent;
            else if (tnParent.Nodes.Count == 0) return null;

            TreeNode tnCurrent, tnCurrentPar;

            //Init node
            tnCurrentPar = tnParent;
            tnCurrent = tnCurrentPar.FirstNode;

            while (tnCurrent != null && tnCurrent != tnParent)
            {
                while (tnCurrent != null)
                {
                    //if (tnCurrent.Text == strValue) return tnCurrent;
                    if (tnCurrent.Text.Contains(strValue)) return tnCurrent;
                    else if (tnCurrent.Nodes.Count > 0)
                    {
                        //Go into the deepest node in current sub-path
                        tnCurrentPar = tnCurrent;
                        tnCurrent = tnCurrent.FirstNode;
                    }
                    else if (tnCurrent != tnCurrentPar.LastNode)
                    {
                        //Goto next sible node
                        tnCurrent = tnCurrent.NextNode;
                    }
                    else
                        break;
                }

                //Go back to parent node till its has next sible node
                while (tnCurrent != tnParent && tnCurrent == tnCurrentPar.LastNode)
                {
                    tnCurrent = tnCurrentPar;
                    tnCurrentPar = tnCurrentPar.Parent;
                }

                //Goto next sible node
                if (tnCurrent != tnParent)
                    tnCurrent = tnCurrent.NextNode;
            }
            return null;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            new frmTestMenu(nodes).ShowDialog();
        }

        private void btnUseMenu_Click(object sender, EventArgs e)
        {
            frmTemplate from = new frmTemplate(txtMenuTitle.Text, nodes.Id);
            DialogResult result = from.ShowDialog();
            if (result == DialogResult.Yes) {
                InitMenu(from.NavigationMenu);
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "62.html");
        }
        //private TreeNode FindNode(TreeNode tnParent, string strValue)
        //{
        //    if (tnParent == null) return null;
        //    if (tnParent.Text == strValue) return tnParent;

        //    TreeNode tnRet = null;
        //    foreach (TreeNode tn in tnParent.Nodes)
        //    {
        //        tnRet = FindNode(tn, strValue);
        //        if (tnRet != null) break;
        //    }
        //    return tnRet;
        //}
    }
}
